package com.ciwong.demoenglish.application;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;

import com.ciwong.demoenglish.BuildConfig;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.pad.contentprovider.LoginToken;
import com.ciwong.epaper.modules.pad.homeworkwidget.common.AppWidgetConstants;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.epaper.util.LauncherSwitcher;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.libs.utils.CWSys;
import com.ciwong.mobilelib.utils.BaseSystem;
import com.ciwong.mobilelib.utils.EAction;

import static com.ciwong.epaper.ui.NewLoginActivity.KEY_NO_PWD_TO_LOGIN_CURRENT_BRAND_ID;

import java.util.List;

/**
 * 演示版Application
 *
 * @author Administrator
 * @version ciwong v.1.0 2016/5/27
 * @since ciwong v.1.0
 */
public class DApplication extends EApplication {

    private static final String POCKET_BUILD_ID = "L5552Y_ZX50_";


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        //需要再attachBaseContext方法初始化，onCreate的调用慢于ContentProvider静态代码块的调用
        LoginToken.AUTHORITY = BuildConfig.APPLICATION_ID + ".login.contentprovider.sqlite";
        LoginToken.CONTENT_URI_STRING = "content://" + LoginToken.AUTHORITY + "/" + LoginToken.PATH_MULTIPLE;
        LoginToken.CONTENT_URI = Uri.parse(LoginToken.CONTENT_URI_STRING);
        LoginToken.ACTION_LOGIN_SUCCESS = BuildConfig.APPLICATION_ID + "." + LoginToken.ACTION_LOGIN_SUCCESS;
        LoginToken.ACTION_EXIT_LOGIN = BuildConfig.APPLICATION_ID + "." + LoginToken.ACTION_EXIT_LOGIN;
        LoginToken.ACTION_BIND_PHONE_SUCCESS = BuildConfig.APPLICATION_ID + "." + LoginToken.ACTION_BIND_PHONE_SUCCESS;

        AppWidgetConstants.ACTION_HAS_LOGIN_SUCESS = BuildConfig.APPLICATION_ID + "." + AppWidgetConstants.ACTION_HAS_LOGIN_SUCESS;
        AppWidgetConstants.ACTION_HAS_JOINED_CLASS = BuildConfig.APPLICATION_ID + "." + AppWidgetConstants.ACTION_HAS_JOINED_CLASS;
        AppWidgetConstants.ACTION_LOAD_LOCAL_HOMEWORK_SUCCESS = BuildConfig.APPLICATION_ID + "." + AppWidgetConstants.ACTION_LOAD_LOCAL_HOMEWORK_SUCCESS;
        AppWidgetConstants.ACTION_LOAD_NET_HOMEWORK_SUCCESS = BuildConfig.APPLICATION_ID + "." + AppWidgetConstants.ACTION_LOAD_NET_HOMEWORK_SUCCESS;
        AppWidgetConstants.ACTION_LOAD_NET_HOMEWORK_FAILED = BuildConfig.APPLICATION_ID + "." + AppWidgetConstants.ACTION_LOAD_NET_HOMEWORK_FAILED;

        AppWidgetConstants.ACTION_WIDGET_LOGIN = BuildConfig.APPLICATION_ID + "." + AppWidgetConstants.ACTION_WIDGET_LOGIN;
        AppWidgetConstants.ACTION_WIDGET_JOIN_CLASS = BuildConfig.APPLICATION_ID + "." + AppWidgetConstants.ACTION_WIDGET_JOIN_CLASS;
        AppWidgetConstants.ACTION_REFRESH_CLICK = BuildConfig.APPLICATION_ID + "." + AppWidgetConstants.ACTION_REFRESH_CLICK;
        AppWidgetConstants.ACTION_REFRESH_ALL_CLICK = BuildConfig.APPLICATION_ID + "." + AppWidgetConstants.ACTION_REFRESH_ALL_CLICK;
        AppWidgetConstants.ACTION_REFRESH_AUTO = BuildConfig.APPLICATION_ID + "." + AppWidgetConstants.ACTION_REFRESH_AUTO;
        AppWidgetConstants.ACTION_JUMP_LISTITEM = BuildConfig.APPLICATION_ID + "." + AppWidgetConstants.ACTION_JUMP_LISTITEM;
        AppWidgetConstants.ACTION_JUMP_MEFRAGMENT = BuildConfig.APPLICATION_ID + "." + AppWidgetConstants.ACTION_JUMP_MEFRAGMENT;

    }

    @Override
    public void onCreate() {
        BRAND_ID = BuildConfig.BRAND_ID;
        OLD_BRAND_ID = BuildConfig.OLD_BRAND_ID;
        TYPE_ID = BuildConfig.TYPE_ID;
        BaseSystem.PROJECT_FOLDER = BuildConfig.PROJECT_FOLDER;
        EConstants.CLIENT_ID = BuildConfig.CLIENT_ID;
        EConstants.CLIENT_SECRECT = BuildConfig.CLIENT_SECRECT;

        //免密码登录专用
        //EConstants.CLIENT_ID = "100151";
        //EConstants.CLIENT_SECRECT = "9a0d19bc3af24f0bbcd9ee386978b619";

        EConstants.NOMOR_CLIENT_ID = EConstants.CLIENT_ID;
        EConstants.NOMOR_CLIENT_SECRECT = EConstants.CLIENT_SECRECT;
        EConstants.IS_YOUKE = BuildConfig.IS_YOUKE;


        EConstants.WEIXIN_APP_ID = BuildConfig.WEIXIN_APP_ID;
        EConstants.QQ_APP_ID = BuildConfig.QQ_APP_ID;
        EAction.UPGRADE_URL = "http://eapi.ciwong.com/repos/" + OLD_BRAND_ID + "/android/update";
        super.onCreate();
        if ("100151".equals(EConstants.CLIENT_ID)) {
            int sharedInt = CWSys.getSharedInt(KEY_NO_PWD_TO_LOGIN_CURRENT_BRAND_ID, 0);
            if (sharedInt != 0) {
                BRAND_ID = sharedInt;
            }
        }

//        if (isLauncherActivity(getApplicationContext(), "com.ciwong.epaper.ui.SplashActivity")) {
//            clearLauncher(getApplicationContext(), "com.ciwong.epaper.ui.SplashActivity");
//        }

        String buildId = android.os.Build.ID != null ? android.os.Build.ID : "";

//        CWLog.e("onCreate", "buildId:" + buildId);
//        if (buildId.contains(POCKET_BUILD_ID)){
//            LauncherSwitcher.switchLauncher(
//                    getApplicationContext(),
//                    "com.ciwong.epaper.ui.SplashActivity", // 启用
//                    "com.ciwong.epaper.ui.SplashActivityDef"  // 禁用
//            );
//        }else {
//            LauncherSwitcher.switchLauncher(
//                    getApplicationContext(),
//                    "com.ciwong.epaper.ui.SplashActivityDef", // 启用
//                    "com.ciwong.epaper.ui.SplashActivity"  // 禁用
//            );
////            if (isLauncherActivity(getApplicationContext(), "com.ciwong.epaper.ui.SplashActivityDef")) {
////                clearLauncher(getApplicationContext(), "com.ciwong.epaper.ui.SplashActivityDef");
////            }
//        }

        //如果包名是"com.ciwong.newspaperhd",设置为平板环境
        if ("com.ciwong.newspaperhd".equals(getPackageName())) {
            EApplication.PLATFORM_ENV = EApplication.PLATFORM_PAD;
        }
        //如果是平板环境
        if (EApplication.PLATFORM_ENV == EApplication.PLATFORM_PAD) {
            EAction.UPGRADE_URL = "https://eapi.ciwong.com/repos/xistudentpad/android/update";
            if (EApplication.RUN_ENV == 2) {
                BRAND_ID = 524726977;
                EConstants.CLIENT_ID = "100021";
                EConstants.NOMOR_CLIENT_SECRECT = EConstants.CLIENT_SECRECT = "22974c33f58df25b3f6570b626e435f9";
            } else {
                BRAND_ID = 570678343;
                EConstants.CLIENT_ID = "100137";
            }
        }
    }

    /**
     * 判断某个 Activity 是否是 Launcher（MAIN + LAUNCHER）
     */
    public static boolean isLauncherActivity(Context context, String activityName) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setPackage(context.getPackageName());

        PackageManager pm = context.getPackageManager();
        List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);

        for (ResolveInfo info : resolveInfos) {
            if (info.activityInfo != null && activityName.equals(info.activityInfo.name)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 清除某个 Activity 的 Launcher 属性（禁用它）
     */
    public static void clearLauncher(Context context, String activityName) {
        if (isLauncherActivity(context, activityName)) {
            ComponentName cn = new ComponentName(context, activityName);
            PackageManager pm = context.getPackageManager();
            pm.setComponentEnabledSetting(
                    cn,
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                    PackageManager.DONT_KILL_APP
            );
        }
    }

}
